/*
 * Package il.ac.biu.cs.grossmm.impl.keys
 * File PatternOperations.java
 * 
 * This is a part of presence service framework prototype. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.impl.keys;

import static il.ac.biu.cs.grossmm.api.keys.KeyPattern.NONE;
import il.ac.biu.cs.grossmm.api.keys.ArrayKeyPattern;
import il.ac.biu.cs.grossmm.api.keys.Attribute;
import il.ac.biu.cs.grossmm.api.keys.KeyPattern;
import il.ac.biu.cs.grossmm.api.keys.PatternEntry;

public class PatternOperations {

	private final static Object NO_OBJECT = new Object() {
		@Override
		public String toString()
		{
			return "NO_OBJECT";
		}
	};

	public static KeyPattern intersection(KeyPattern pattern1,
		KeyPattern pattern2)
	{
		// find intersection of values and/or classes
		Object o = valueIntersection(pattern1, pattern2);

		if (o == NO_OBJECT)
			return NONE;

		boolean asValue =
			!(o instanceof Class) || pattern1.valueClass() == null
				|| pattern2.valueClass() == null;

		boolean isMask1 = pattern1.isMask();
		boolean isMask2 = pattern2.isMask();

		// intersection of two patterns is a mask pattern iff
		// both of the two patterns are mask patterns

		boolean isMask = isMask1 && isMask2;

		// the result pattern

		ArrayKeyPattern result = new ArrayKeyPattern(isMask, o, asValue);

		int size1 = pattern1.size();

		// first pass (iterate attributes of pattern1)
		for (int i = 0; i < size1; i++) {
			PatternEntry pe1 = pattern1.entry(i);
			Attribute a1 = pe1.getAttribute();

			PatternEntry pe2 = pattern2.entry(a1);

			if (pe2 == null) {
				// attribute a1 belongs to the left pattern but
				// does not belong to the righ pattern
				if (isMask2)
					result.add(pe1);
				else if (pe1.isMandatory())
					return NONE;
				// otherwise add nothing

			} else {
				KeyPattern sp1 = pe1.getPattern();
				KeyPattern sp2 = pe2.getPattern();

				KeyPattern sub = intersection(sp1, sp2);
				boolean anyMandatory = pe1.isMandatory() || pe2.isMandatory();

				if (sub == NONE && anyMandatory)
					return NONE;

				if (!isMask || sub != NONE)
					result.add(a1, sub, anyMandatory);
			}
		}

		int size2 = pattern2.size();

		// second pass (iterate attributes of pattern2)
		for (int i = 0; i < size2; i++) {
			PatternEntry pe2 = pattern2.entry(i);
			Attribute a2 = pe2.getAttribute();

			// check only such entries in pattern2 where entry attribute
			// does not appear in pattern1
			if (pattern1.entry(a2) != null)
				continue;

			if (isMask1)
				result.add(pe2);
			else if (pe2.isMandatory())
				return NONE;
		}

		return result;
	}

	private static Object valueIntersection(KeyPattern pattern1,
		KeyPattern pattern2)
	{
		Object result;

		Class class1 = pattern1.valueClass();
		Class class2 = pattern2.valueClass();

		if (class1 == null) {
			// left pattern specifies a value, not a class

			Object value1 = pattern1.value();
			if (class2 == null) {
				// right pattern specifies a value also
				Object value2 = pattern2.value();
				if (value1 == null ? value2 == null : value1.equals(value2))
					result = value1;
				else
					result = NO_OBJECT;
			} else {
				// right pattern specifies a class
				if (value1 == null
					|| class2.isAssignableFrom(value1.getClass()))
					result = value1;
				else
					result = NO_OBJECT;
			}
		} else {
			// left pattern specifies a class

			if (class2 == null) {
				// right pattern specifies a value
				Object value2 = pattern2.value();
				if (value2 == null
					|| class1.isAssignableFrom(value2.getClass()))
					result = value2;
				else
					result = NO_OBJECT;
			} else {
				// right pattern specifies a class also
				if (class1.equals(class2))
					result = class1;
				else if (class1.isAssignableFrom(class2))
					result = class2;
				else if (class2.isAssignableFrom(class1))
					result = class1;
				else
					result = null; // null belongs to every class
			}
		}

		return result;
	}

	/**
	 * Given a target pattern, out and in patterns, for each entry PE in target
	 * pattern if there is an entry PE' in the out pattern with the same
	 * attribute, removes this entry and then adds all entries from the in
	 * pattern, replacing any entries with the same attributes.
	 * <p>
	 * in and out patterns must be mask patterns, all their entries should be
	 * mandatory. out pattern not refer to value-classes (true recusrively)
	 * 
	 * @param pattern
	 * @param mask
	 * @return
	 */
	public static KeyPattern transform(KeyPattern target, KeyPattern out,
		KeyPattern in)
	{
		boolean asValue = target.valueClass() == null;
		ArrayKeyPattern result =
			new ArrayKeyPattern(target.isMask(), asValue ? target.value()
				: target.valueClass(), asValue);

		int size = target.size();

		// add all entries from target where entry attribute does
		// not match any entry in the in or out patterns
		for (int i = 0; i < size; i++) {
			PatternEntry e = target.entry(i);
			Attribute a = e.getAttribute();

			if (in.entry(a) == null && out.entry(a) == null)
				result.add(e);
		}

		size = in.size();

		for (int i = 0; i < size; i++) {
			result.add(in.entry(i));
		}

		return result;
	}
}
